package com.xiaoyao.novel.util;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.common.Constant;
import com.xiaoyao.novel.common.Pagin;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Category;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.service.BookService;
import com.xiaoyao.novel.service.CategoryService;
import com.xiaoyao.novel.service.ChapterService;
import com.xiaoyao.novel.service.WebInfoService;

/**
 * 
 * @deprecated 废弃,使用 freeMark静态化  <BR>
 * book-id.html chapter-id.html index.html 静态化. <BR>
 * 
 * @author kuangmingai
 */
public class HtmlStatic {
	static Logger logger = Logger.getLogger(HtmlStatic.class);

	public static String encodeHtml(int id) {
		return id + ".html";
	}

	public static String encodeCategory(int id) {
		return FileUtil.listPath + id + ".html";
	}

	public static String encodeBook(int id) {
		return FileUtil.bookPath + id + ".html";
	}

	public static String encodeChapter(int id) {
		return FileUtil.chapterPath + id + ".html";
	}

	static BookService bookService = new BookService();
	static ChapterService chapterService = new ChapterService();
	static CategoryService categoryService= new CategoryService();

	public static void staticBooks(List<Book> bookList) {
		logger.warn("静态化所有小说:"+bookList.size()) ;
		for (Book book : bookList) {
			staticBook(book);
		}
	}

	// 分类
	public static void staticCategory(List<Category> list) {
		for (Category category : list) {
			String fileName = encodeHtml(category.cateId);
			String filePath = FileUtil.getWebRoot() + FileUtil.listPath + fileName;
			String httpUri = "/list.do?category.cateId=" + category.cateId;
			staticHtml(httpUri, filePath);
		}
	}
	// 分类
	public static void staticCategorys() {
		List<Category> list=categoryService.getAll();
		staticCategory(list);
	}

	public static String staticCategory(int cateId) {
		String fileName = encodeHtml(cateId);
		String filePath = FileUtil.getWebRoot() + FileUtil.listPath + fileName;
		String httpUri = "/list.do?category.cateId=" + cateId;
		return staticHtml(httpUri, filePath);
	}

	// ////////
	public static String staticBook(Book book) {
		if (book==null) {
			return "error:书籍为空";
		}
		String fileName = encodeHtml(book.bookId);
		String filePath = FileUtil.getWebRoot() + FileUtil.bookPath + fileName;
		String httpUri = "/book.do?book.bookId=" + book.bookId;
		logger.info("静态化小说:" + book.bookName/*+" "+ httpUri + "--->" + filePath */ );
		return staticHtml(httpUri, filePath);
	}

	public static String staticBook(int bookId) {
		Book book = bookService.getById(bookId);
		return staticBook(book);
	}

	public static void staticChapters(int bookId) {
		Pagin pagin = new Pagin();
		pagin.setPageSize(10000);
		pagin.getSearchParam().put("bookId", bookId + "");
		List<Chapter> chapters = chapterService.search(pagin);
		for (Chapter chapter : chapters) {
			String fileName = encodeHtml(chapter.cid);
			String filePath = FileUtil.getWebRoot() + FileUtil.chapterPath + fileName;
			// 文件大于5KB的不用静态化
			File file =new File(filePath);
			if ( file.exists() && file.length()> 5 *KB_SIZE) {
				continue;
			}
			
			String httpUri = "/chapter.do?chapter.cid=" + chapter.cid;
			staticHtml(httpUri, filePath);
		}
	}

	public static final long KB_SIZE=1024 ;
	// //////
	public static String staticChapter(int cid,boolean force) {
		Chapter chapter = chapterService.getById(cid);
		return 	staticChapter(chapter,  force); 
	}

	public static String staticChapter(Chapter chapter,boolean force) {
		if (chapter==null) {
			return "error:章节为空";
		}
		String fileName = encodeHtml(chapter.cid);
		String filePath = FileUtil.getWebRoot() + FileUtil.chapterPath + fileName;
		// 文件大于5KB的不用静态化
		File file =new File(filePath);
		if ( !force && file.exists() && file.length()> 5 *KB_SIZE) {
			return "";
		}
		String httpUri = "/chapter.do?chapter.cid=" + chapter.cid;
		logger.info("静态化Chapter:" + chapter.bookId + "/" + chapter.cid/* + " " + httpUri + "--->" + filePath*/);
		return staticHtml(httpUri, filePath);
	}

	public static void staticIndex() {
		String fileName = "index.html";
		String filePath = FileUtil.getWebRoot() + fileName;
		String httpUri = "/index.do";
		logger.info("静态化首页:"/* + httpUri + "--->" + filePath*/);
		staticHtml(httpUri, filePath);
	}

	static HttpUtil httpUtil = new HttpUtil(Constant.CHARSET);
	static WebInfoService webInfoService=new WebInfoService();
	static final int MIN_PAGE_SIZE=4*1024 ; // 页面最小为 4K
	/**
	 * @param httpUri
	 *            网络地址
	 * @param filePath
	 *            绝对路径
	 */
	public static String staticHtml(String httpUri, String filePath) {
		String content = "";
		String ip =webInfoService.getStaticWebInfo().getDomain();
		String port = Constant.PORT;
		String httpUrl =ip+port+ Constant.WEBROOT + httpUri;
		try {
			content = httpUtil.getPage(httpUrl);
			//logger.warn("静态化-"+filePath+":"+content);
			if (content.length()>MIN_PAGE_SIZE) {
				FileUtil.saveAbsolutlyFile( filePath, content);
			}
		} catch (IOException e) {
			logger.error(e+httpUrl);
		}
		return content;
	}

	public static int removeChapters() {
		int size =0 ;
		File file = new File(FileUtil.getWebRoot() + FileUtil.chapterPath);
		if (file.exists() && file.isDirectory()) {
			File[] files = file.listFiles();
			size=files.length;
			for (int i = 0; i < size; i++) {
				files[i].delete();
			}
		}
		return size ;
	}

	public static void removeBooks() {
		File file = new File(FileUtil.getWebRoot() + FileUtil.bookPath);
		if (file.exists() && file.isDirectory()) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				files[i].delete();
			}
		}
	}
	
	public static void main(String[] args) {
		File file =new File("D:/360安全浏览器下载/小Q签到.zip") ;
		System.out.println(file.length()); 
	}

}
